package com.easy.service.impl;


import com.easy.zhu.LogMethod;
import com.easy.bean.UserBean;
import com.easy.mapper.UserBeanDao;
import com.easy.service.UserBeanService;
import com.easy.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserBeanService {
    @Autowired
    UserBeanDao userBeanDao;
    @Override
    @LogMethod(serviceName = "用户添加处理")
    public UserBean add(UserBean userBean) {


        String password=userBean.getUser_code();
        userBean.setUser_code(PasswordUtil.getEncrypyion(password));
        userBeanDao.add(userBean);
        UserBean result = userBeanDao.one(userBean.getId());
        return result;
    }

//    @CacheEvict(value = "user",key="'one'+#p0")
    @Override
    @LogMethod(serviceName = "用户删除处理")
    public int del(Integer id) {
        return userBeanDao.del(id);
    }

    @CachePut(value = "user",key="'one'+#p0.user_id")
    @Override
    @LogMethod(serviceName = "用户修改处理")
    public UserBean edit(UserBean userBean) {
        System.out.println(userBean);
        userBeanDao.edit(userBean);
        UserBean result = userBeanDao.one(userBean.getId());
        return result;
    }

    @Cacheable(value = "user",key="'one'+#user_id")
    @Override
    @LogMethod(serviceName = "用户查询处理")
    public UserBean one(Integer id) {
        return userBeanDao.one(id);
    }

    @Override
    @LogMethod(serviceName = "用户查询处理")
    public List<UserBean> all(UserBean userBean) {
        return userBeanDao.all(userBean);
    }

    @Override
    @LogMethod(serviceName = "用户分页处理")
    public void page(Page page, UserBean userBean) {
        int count=userBeanDao.pageCount(userBean);
        Object data=userBeanDao.page(page,userBean);
        page.setCount(count);
        page.setData(data);
    }

    @Override
    @LogMethod(serviceName = "用户密码修改处理") // 补充日志注解，统一风格
    public Result changePassword(String user_code, String newPassword, String confirmPassword,String token) {
        // 先确认用户已登录
        if (!userTokenUtil.verify(token)) {
            return Result.fail(401, "重新登录");
        }

        // 从 Token 中获取用户唯一标识
        Object userObj = userTokenUtil.getOption(token, SYS.TOKEN_USER_ID);
        System.out.println(userObj);
        if (userObj == null) {
            return Result.fail(401, "重新登录");
        }

        // 验证新密码与确认密码一致性
        if (newPassword == null || confirmPassword == null || !newPassword.equals(confirmPassword)) {
            return Result.fail(400, "新密码与确认密码不一致");
        }

        // 根据用户 ID 查询数据库中的用户
        UserBean userBean = userBeanDao.one(Integer.parseInt(userObj.toString()));
        if (userBean == null) {
            return Result.fail(400, "用户不存在或已被删除");
        }

        //验证原密码正确性
        String encryptedOldPassword = PasswordUtil.getEncrypyion(user_code);
        if (!PasswordUtil.checkPassword(user_code,userBean.getUser_code())) {
            return Result.fail(400, "原密码错误");
        }

        //加密新密码并更新到数据库
        String encryptedNewPassword = PasswordUtil.getEncrypyion(newPassword);
        UserBean userBean1=new UserBean();
        userBean1.setId(Integer.parseInt(userObj.toString()));//根据主键修改原密码
        userBean1.setUser_code(encryptedNewPassword); // 设置加密后的新密码
        userBeanDao.edit(userBean1); //执行更新

        // 返回结果
        return Result.success("密码修改成功");
    }

}
